﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Dynamics.BusinessConnectorNet;

namespace PJ.Framework.DynamicsAX
{
    public class AxConnection : IDisposable
    {
        private Axapta m_Axapta;
        private static readonly AxConnection m_Instance = new AxConnection();

        public static AxConnection Instance
        {
            get
            {
                return m_Instance;
            }
        }
        private Axapta Axapta
        {
            get
            {
                if (m_Axapta == null)
                {
                    m_Axapta = new Axapta();
                    Open();
                }
                return m_Axapta;
            }
        }
        public void Open()
        {
            Axapta.Logon("", "", "", "");
        }
        public void Close()
        {
            if (m_Axapta != null)
            {
                m_Axapta.Logoff();
            }
        }

        public bool Opened
        {
            get
            {
                if (m_Axapta == null)
                    return false;
                return m_Axapta.Session() != 0;
            }
            set
            {
                if (value && !Opened)
                    Open();
                if (!value && Opened)
                    Close();
            }
        }


        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        public void Dispose(bool disposable)
        {
            if (m_Axapta != null && disposable)
            {
                m_Axapta.Dispose();
                m_Axapta = null;
            }
        }

        public object StaticMethod(string className, string method)
        {
            return Axapta.CallStaticClassMethod(className, method);
        }
        public object StaticMethod(string className, string method, object argument)
        {
            return Axapta.CallStaticClassMethod(className, method, argument);
        }
        public object StaticMethod(string className, string method, object argument1, object argument2)
        {
            return Axapta.CallStaticClassMethod(className, method, argument1, argument2);
        }
        public AxaptaContainer CreateContainer()
        {
            return Axapta.CreateAxaptaContainer();
        }

        public AxaptaRecord find(string tableName, string id)
        {
            try
            {
                return (AxaptaRecord)Axapta.CallStaticRecordMethod(tableName, "find", id, false);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public AxaptaRecord select(string tableName, string selectStatement)
        {
            AxaptaRecord record = Axapta.CreateAxaptaRecord(tableName);
            try
            {
                Axapta.ExecuteStmt(selectStatement, record);
            }
            catch(Exception)
            {
                throw;
            }
            return record;
        }

    }
}
